package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.Iterator;

public class DisplayBooksState implements State {

    private static DisplayBooksState displayBooksStateInstance;
    Library books = Library.getLibraryInstance();

    private DisplayBooksState() {

    }

    public static synchronized DisplayBooksState getDisplayBooksStateInstance() {
        if (displayBooksStateInstance == null) {

           //this
            displayBooksStateInstance = new DisplayBooksState();
        }

        return displayBooksStateInstance;

    }

    @Override
    public State change(String userInput) {
        if (userInput.equals("r"))
            return ReserveBookState.getReserveBookStateInstance();

        System.out.println(INVALID_INPUT);
        return DisplayBooksState.getDisplayBooksStateInstance();
    }

    @Override
    public String displayMenuOptions() {
        String bookTitleAndAvailability = new String();
        Iterator bookIterator = books.getBooks().iterator();
        String availabilityString = "Unavailable";

        while (bookIterator.hasNext()) {
            Book aBook = (Book)bookIterator.next();
            if (aBook.getAvailability())
                availabilityString = "Available";

            bookTitleAndAvailability = bookTitleAndAvailability + aBook.getTitle() + " " + availabilityString + "\n";
        }

        return bookTitleAndAvailability;
    }

    @Override
    public String displayWelcomeMessage() {
        return "Here are the books that are available:\nPress \"r\" to reserve a book.";
    }
}
